package sziit.znk.services.impl;

import sziit.znk.services.BeanService;
import sziit.znk.factory.BeanFactory;
import sziit.znk.utils.exception.BeanFactoryException;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class OptBeanContainer implements BeanService {

    private Map<String, Object> beanContainer = BeanFactory.getBeanContainer();

    @Override
    public Object getBean(String id) {
        Object o = beanContainer.get(id);
        try {
            if (o == null) throw new BeanFactoryException();
        } catch (BeanFactoryException e) {
            e.showGetBeanException();
        }
        return o;
    }

    @Override
    public Set<String> getAllBeans() {
        Iterator<String> iterator = beanContainer.keySet().iterator();
        Set<String> beans = new HashSet<>();
        while (iterator.hasNext()) {
            beans.add(iterator.next());
        }
        return beans;
    }

}
